<template>
  <el-dialog :visible.sync="dialogVisible" title="选择排产信息" width="1200px" append-to-body @close="handleClose">
    <el-form ref="query" :model="page.query" inline label-position="right" label-width="80px">
      <el-form-item label="排产编号" prop="produceNo">
        <el-input v-model="page.query.produceNo" placeholder="请输入排产编号" />
      </el-form-item>
      <el-form-item label="产品名称" prop="productId">
        <product-query ref="productQuery" v-model="page.query.productId" placeholder="请输入产品名称" @choose="handleChooseProduct" />
      </el-form-item>
      <el-form-item label="客户名称" prop="customerId">
        <customer-query ref="customerQuery" v-model="page.query.customerId" @choose="handleChooseCustomer" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
        <el-button icon="el-icon-refresh" @click="resetQueryForm">重置</el-button>
      </el-form-item>
    </el-form>
    <!--      <div>
        <span>排产信息列表</span>
      </div>
      <el-divider />-->
    <div class="table-button">
      <el-tooltip content="刷新" class="table-right-btn">
        <el-button size="mini" circle icon="el-icon-refresh" @click="doPage" />
      </el-tooltip>
    </div>
    <el-table
      ref="table"
      v-loading="tableLoading"
      :data="tableData"
      style="width: 100%"
      border
      @row-dblclick="chooseClick"
      @select="handleSelect"
    >
      <el-table-column type="selection" width="45" />
      <el-table-column label="排产编号" prop="produceNo" min-width="120" />
      <el-table-column label="生产日期" prop="produceDate" min-width="100" />
      <el-table-column label="客户名称" prop="customerName" min-width="100" />
      <el-table-column label="产品名称" prop="productName" min-width="100">
        <template slot-scope="scope">
          {{ scope.row.productInfo.moldName }}
        </template>
      </el-table-column>
      <el-table-column label="颜色" prop="color" min-width="60">
        <template slot-scope="scope">
          {{ scope.row.productInfo.color }}
        </template>
      </el-table-column>
      <el-table-column label="材质" prop="material" min-width="60">
        <template slot-scope="scope">
          {{ scope.row.productInfo.material }}
        </template>
      </el-table-column>
      <el-table-column label="尺码" prop="size" min-width="60">
        <template slot-scope="scope">
          {{ scope.row.productInfo.size }}
        </template>
      </el-table-column>
      <el-table-column label="长度" prop="length" min-width="100">
        <template slot-scope="scope">
          {{ scope.row.productInfo.length }}
        </template>
      </el-table-column>
      <el-table-column label="总数量" prop="quantity" min-width="80" />
      <el-table-column label="剩余数量" prop="remainingQuantity" min-width="80" />
      <el-table-column label="优先级" prop="priority" min-width="100">
        <template slot-scope="scope">
          <priority-tag v-model="scope.row.priority" />
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" min-width="60">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="chooseClick(scope.row)"
          >选择
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :page-sizes="[10, 20, 30, 40]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="handleClose">取消</el-button>
      <el-button type="primary" @click="chooseClick(selected[0])">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { selectProducePage } from '@/api/erp/produce'

export default {
  name: 'ProduceModule',
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      tableData: [],
      tableLoading: false,
      selected: [],
      page: {
        page: 1,
        size: 10,
        total: 0,
        query: {
          productId: '',
          produceState: '0'
        }
      },
      dialogVisible: this.visible
    }
  },
  methods: {
    handleSearch() {
      this.page.page = 1
      this.doPage()
    },
    doPage() {
      // 分页查询
      this.tableLoading = true
      selectProducePage(this.page).then(resp => {
        if (resp.success) {
          this.tableData = resp.data.records
          this.page.total = resp.data.total
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    // 选中事件
    handleSelect(row) {
      this.selected = []
      if (row.length > 1) {
        this.$refs.table.clearSelection()
        this.$refs.table.toggleRowSelection(row[row.length - 1])
      }
      this.selected = [row[row.length - 1]]
    },
    resetQueryForm() {
      this.$refs.query.resetFields()
      this.page.query.customerId = undefined
      this.page.query.productId = undefined
      this.$refs.productQuery.resetQueryForm()
      this.$refs.customerQuery.resetQueryForm()
      this.doPage()
    },
    chooseClick(row) {
      this.$emit('close', row)
    },
    handleClose() {
      this.$emit('close')
    },
    // 分页大小
    handleSizeChange(val) {
      this.page.size = val
      this.doPage()
    },
    // 页码
    handleCurrentChange(val) {
      this.page.page = val
      this.doPage()
    },
    handleChooseProduct(row) {
      if (row) {
        this.page.query.productId = row.id
      }
    },
    handleChooseCustomer(row) {
      if (row) {
        this.page.query.customerId = row.id
      }
    }
  }
}
</script>

<style scoped>

</style>
